package com.jf.plugin.wbcs;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jf.core.Jbsf;
import com.jf.core.JbsfFilter;
import com.jf.plugin.IPlugin;
import com.wbcs.config.Config;
import com.wbcs.config.ConfigLoadManager;
import com.wbcs.config.database.datasource.AbsDataSource;
import com.wbcs.system.assistant.WbcsAssistant;
import com.wbcs.system.dataimport.thread.FileUpDataImportThread;
import com.wbcs.system.dataimport.thread.TimingDataImportThread;
import com.wbcs.util.Tools;
import com.wbcs.util.WbcsClassLoader;

/**
 <filter>
    <filter-name>jbsf</filter-name>
    <filter-class>com.jf.core.JbsfFilter</filter-class>
    <init-param>
      <param-name>configClass</param-name>
      <param-value>conf.JbsfIVConfig</param-value>
    </init-param>
    <init-param>
      <param-name>configpath</param-name>
      <param-value>classpath{/com/pansoft/jbsf/wbcs}</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>jbsf</filter-name>
    <url-pattern>/jf/*</url-pattern>
  </filter-mapping>
  
 * @author hawkfly
 *
 */
public class WbcsPlugin implements IPlugin {

	private static Log log=LogFactory.getLog(WbcsPlugin.class);
	private boolean isStarted = false;
	private ServletContext context;
	
	public boolean start() {
		// TODO Auto-generated method stub
		if (isStarted)
			return true;
		//启动文件上传及数据导入现成，执行跑批操作
		FileUpDataImportThread.getInstance().start();
        TimingDataImportThread.getInstance().start();
        //初始化插件参数
        this.context=Jbsf.me().getServletContext();
        closeAllDatasources();
        
        Config.homeAbsPath=context.getRealPath("/");
        Config.homeAbsPath=Tools.standardFilePath(Config.homeAbsPath+"\\");
        Config.configpath=JbsfFilter.filterConfig.getInitParameter("configpath");//配置文件存放的物理路径
        if(Config.configpath==null||Config.configpath.trim().equals(""))
        {
            log.info("没有配置存放配置文件的根路径，将使用路径"+Config.homeAbsPath+"做为配置文件的根路径");
            Config.configpath=Config.homeAbsPath;
        }else
        {
            Config.configpath=WbcsAssistant.getInstance().parseConfigPathToRealPath(
                    Config.configpath,Config.homeAbsPath);
        }
        loadReportConfigFiles(null);
        
        isStarted = true;
		return true;
	}

	public boolean stop() {
		// TODO Auto-generated method stub
		closeAllDatasources();
        FileUpDataImportThread.getInstance().stopRunning();
        TimingDataImportThread.getInstance().stopRunning();
        
        this.context=null;
        isStarted = false;
		return true;
	}
	
	public static void loadReportConfigFiles(String type)
    {
        log.info("正在加载配置文件wbcs.cfg.xml及所有报表配置文件...");
        
        if("updateconfig".equals(type) || ConfigLoadManager.currentDynClassLoader == null)
        {
        	ConfigLoadManager.currentDynClassLoader=new WbcsClassLoader(Thread.currentThread().getContextClassLoader());
        }
        
        int flag=ConfigLoadManager.loadAllReportSystemConfigs();
        if(flag==-1)
        {
            log.error("加载报表配置文件wbcs.cfg.xml失败");
        }else if(flag==0)
        {
            log.warn("报表配置文件wbcs.cfg.xml内容为空，或没有配置报表");
        }
    }
	
	private void closeAllDatasources()
    {
        Map<String,AbsDataSource> mDataSourcesTmp=Config.getInstance().getMDataSources();
        if(mDataSourcesTmp!=null)
        {
            for(Entry<String,AbsDataSource> entry:mDataSourcesTmp.entrySet())
            {
                if(entry.getValue()!=null)
                    entry.getValue().closePool();
            }
        }
    }

}
